<?php
namespace Bshop\Model;
use Think\Model;
class ActivityModel extends Model{
	protected $_validate = array(
			array('name','require','活动名称不能为空'),
			array('class_id','Ckclassid','请选择活动所属二级分类',1,'callback',3),
			array('photo','require','请上传活动图片'),
			array('basc_photo','require','请选择封面图片'),
			array('circle_id','number','请选择活动商圈'),
			array('place','require','请输入活动场地'),
			array('address','require','请输入活动地址'),
			array('east','require','请输入活动坐标'),
			array('phone','isTel','联系电话填写错误',0,'callback',1),
			array('body','require','请输入活动介绍'),
			array('apply_notice','require','请输入活动参与须知'),
			array('fee_type,time_type,wai_activity_start_date,activity_money,url','Ckactivity','时间、相关费用一栏填写错误',1,'callback',3),
			array('buy_limit,before_time,appoint_start_time,appoint_end_time','ckBuy','购买限制填写错误',1,'callback',3),
	);
	protected $_auto = array (
		array('photo','photosort',3,'callback',1),
		/* array('name','trimphoto',3,'callback',1), */
		array('phone','trimphoto',3,'callback',1),
		//array('apply_notice','trimphoto',3,'callback',1),
		array('east','trimphoto',3,'callback',1),
		//array('north','trimphoto',3,'callback',1),
		//array('apply_notice','hang',3,'callback',1),
	);
	public function isTel($tel)
	{
		if(empty($tel)){
			return true;
		}else{
			$regxArr = array(
					'sj'  =>  '/^(\+?86-?)?(18|15|13)[0-9]{9}$/',
					'tel' =>  '/^(010|02\d{1}|0[3-9]\d{2})-\d{7,9}(-\d+)?$/',
					'400' =>  '/^400(-\d{3,4}){2}$/',
			);
			foreach($regxArr as $regx)
			{
				if(preg_match($regx, $tel ))
				{
					return true;
				}
			}
			return false;
		}
		
	}
	public function trimphoto($str){
		return preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/","", strip_tags($str));
	}
	public function strstr($str){
		return str_replace('"',"'",$str);
	}
	public function hang($str){
		return str_replace(' ','\r\n', $str);
	}
	public function photosort($photo){
		$photoarray = explode(",", $photo);
		$bascphoto = $_POST['basc_photo'];
		$key = array_search($bascphoto, $photoarray);
		if($key!='0'){
			$newphoto = $photoarray;
			$newphoto[0] = $bascphoto;
			$newphoto[$key] = $photoarray[0];
			return join(",",$newphoto);
		}else{
			return $photo;
		}
	}
	public function Ckclassid($classid){
		$re = M('activity_classify')->where("id='{$classid}'")->getField('parent_id');
		if(empty($re)){
			return false;
		}else{
			return true;
		}
	}
	protected function ckBuy($data){
		if(preg_match('/^\d+$/',$data['buy_limit'])){
			if($data['time_type'] == 2){
				if(!preg_match('/^\d+$/', $data['before_time'])){
					return false;
				}else{
					return true;
				}
			}elseif ($data['buy_limit'] == 3){
				if(!preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$/', $data['appoint_start_time'])){
					return false;
				}
				elseif(!preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$/', $data['appoint_end_time'])){
					return false;
				}
				elseif($data['appoint_end_time']<=$data['appoint_start_time']){
					return false;
				}else{
					return true;
				}
			}else{
				return true;
			}
		}else{
			return false;
		}
	}
	protected function Ckactivity($data){
		if(!empty($data['fee_type'])){
			if($data['fee_type'] == 3){
				if(empty($data['url']) || empty($data['wai_activity_start_date']) || empty($data['activity_money'])){
					return false;
				}else{
					return true;
				}
			}else{
				if(empty($data['time_type'])){
					return false;
				}else{
					return true;
				}
			}
			
		}else{
			return false;
		}
	}
}